package com.suse.service.view.edu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.suse.common.config.response.R;
import com.suse.service.view.edu.model.entity.Subject;
import com.suse.service.view.edu.mapper.SubjectMapper;
import com.suse.service.view.edu.model.vo.SubjectVo;
import com.suse.service.view.edu.service.SubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-08-16
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Override
    public R getSubjectTreeList() {
        // 获取一级课程分类列表
        List<Subject> firstSubjectList = getFirstSubjectList();
        // 获取二级课程分类列表
        List<Subject> secondSubjectList = getSecondSubjectList();
        // 获取一级课程分类ID与二级课程分类视图对象列表的映射
        Map<String, List<SubjectVo>> map = getSecondSubjectVoMap(secondSubjectList);
        // 建立一级二级树形关系
        List<SubjectVo> firstSubjectVoList = new ArrayList<>();
        for (Subject firstSubject : firstSubjectList) {
            SubjectVo firstSubjectVo = BeanUtil.copyProperties(firstSubject, SubjectVo.class);
            List<SubjectVo> secondSubjectVoList = map.get(firstSubject.getId());
            firstSubjectVo.setChildren(secondSubjectVoList);
            firstSubjectVoList.add(firstSubjectVo);
        }
        return R.success(firstSubjectVoList);
    }

    @Override
    public R getSubjectFirstListAndFirstIdSecondListMap() {
        // 获取一级课程分类列表
        List<Subject> firstSubjectList = getFirstSubjectList();
        // 获取二级课程分类列表
        List<Subject> secondSubjectList = getSecondSubjectList();
        // 获取一级课程分类ID与二级课程分类视图对象列表的映射
        Map<String, List<SubjectVo>> map = getSecondSubjectVoMap(secondSubjectList);
        return R.put("firstList",firstSubjectList).put("secondList",secondSubjectList).put("firstIdSecondListMap",map).success();
    }

    /**
     * 获取一级课程分类列表
     * @return 一级课程分类列表
     */
    private List<Subject> getFirstSubjectList() {
        return lambdaQuery()
                .select(Subject::getId,
                        Subject::getTitle)
                .eq(Subject::getParentId, "0")
                .list();
    }

    /**
     * 获取二级课程分类列表
     * @return 二级课程分类列表
     */
    private List<Subject> getSecondSubjectList() {
        return lambdaQuery()
                .select(Subject::getId,
                        Subject::getTitle,
                        Subject::getParentId)
                .ne(Subject::getParentId, "0")
                .list();
    }

    /**
     * 获取一级课程分类ID与二级课程分类视图对象列表的映射
     * 建立一级课程分类ID与二级课程分类对象列表的映射关系
     * 降低时间复杂度
     * @return 一级课程分类ID与二级课程分类视图对象列表的映射
     */
    private Map<String,List<SubjectVo>> getSecondSubjectVoMap(List<Subject> secondSubjectList) {
        Map<String, List<SubjectVo>> map = new HashMap<>();
        for (Subject subject : secondSubjectList) {
            String parentId = subject.getParentId();
            if (map.containsKey(parentId)) {
                List<SubjectVo> subjectVoList = map.get(parentId);
                SubjectVo subjectVo = BeanUtil.copyProperties(subject, SubjectVo.class);
                subjectVoList.add(subjectVo);
            }else {
                List<SubjectVo> subjectVoList = new ArrayList<>();
                SubjectVo subjectVo = BeanUtil.copyProperties(subject, SubjectVo.class);
                subjectVoList.add(subjectVo);
                map.put(parentId,subjectVoList);
            }
        }
        return map;
    }

}
